package com.umeox.auto_update

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import me.rosuh.filepicker.config.FilePickerManager

class AutoUpdateConfigureActivity : AppCompatActivity(),
    DeviceTypeSelectBottomSheetDialog.DeviceTypeSelectCallback {

    private val dialog = DeviceTypeSelectBottomSheetDialog()

    private lateinit var etDeviceName: EditText
    private lateinit var etDeviceVersions: EditText
    private lateinit var etDeviceTargetVersion: EditText
    private lateinit var tvFileAddress: TextView

    private var otaFile: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_auto_update_configure)
        etDeviceName = findViewById(R.id.etDeviceName)
        etDeviceVersions = findViewById(R.id.etDeviceVersions)
        etDeviceTargetVersion = findViewById(R.id.etDeviceTargetVersion)
        tvFileAddress = findViewById(R.id.tvFileAddress)
        dialog.init(this)
        dialog.setDeviceTypeSelectCallback(this)

        findViewById<ImageView>(R.id.ivSearch).setOnClickListener {
            dialog.show()
        }

        findViewById<TextView>(R.id.btFileSelector).setOnClickListener {
            FilePickerManager
                .from(this)
                .enableSingleChoice()
                .registerFileType(arrayListOf(AutoUpdateOTAFileType()), true)
                .forResult(100)
        }

        findViewById<Button>(R.id.btOta).setOnClickListener {
            val deviceName = etDeviceName.text.toString()
            //检查相关参数
            if (TextUtils.isEmpty(deviceName)) {
                Toast.makeText(this, "请填写蓝牙名称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            val deviceVersions = etDeviceVersions.text.toString()
            if (TextUtils.isEmpty(deviceVersions)) {
                Toast.makeText(this, "请填写待升级版本号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            val deviceTargetVersion = etDeviceTargetVersion.text.toString()
            if (TextUtils.isEmpty(deviceTargetVersion)) {
                Toast.makeText(this, "请填写升级版本号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (TextUtils.isEmpty(otaFile)) {
                Toast.makeText(this, "请选择ota文件", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            val info = AutoUpdateConfigureInfo(
                deviceName,
                deviceVersions.split(",").toList(),
                deviceTargetVersion,
                otaFile
            )
            AutoUpdateManager.setConfigureInfo(info)
            startActivity(Intent(this, AutoUpdateActivity::class.java))
        }
    }

    override fun onDeviceTypeSelect(deviceName: String) {
        etDeviceName.setText(deviceName)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            100 -> {
                if (resultCode == RESULT_OK && data != null) {
                    val list = FilePickerManager.obtainData()
                    if (list.isNotEmpty()) {
                        otaFile = list[0]
                        tvFileAddress.text = otaFile
                    }
                }
            }
        }
    }
}